import Vue from "vue"
import Vuex from "vuex"
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)
const store = new Vuex.Store({
  Plugins: [createPersistedState({
    key:'TODO-list',
  })],
 
  state: {
    list: [],
    getSel: 'all',
    taskName: '',
    isDone:false
  },
  mutations: {
    addTask(state,taskName) {
      state.list.push({
        id:Date.now(),
        name:taskName,
        isDone:false
      })
      state.taskName=''
    },
    delete(state, id) {
      state.list=state.list.filter(item=>item.id !==id)
    },
    clearFn(state){

      state.list= state.list.filter(item=> !item.isDone)
      
    },
    changeFn(state, type) {
      state.getSel=type
    }
  },
  actions: {},
  getters: {
    list: (state) => {
      if(state.getSel==="yes"){
        return state.list.filter(item=>item.isDone)
        // // 如果是 no未完成 把未完成的筛选出来 返回
      }else if(state.getSel==="no"){
        return state.list.filter(item=> !item.isDone)
      }else{
        // 否则·显示全部
        return state.list
      }
      }
  }
  
})
export default store